﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Inovout.Web.Mvc
{
    public static class ListItemExtensions
    {
        public static MvcHtmlString ListItemLink(this HtmlHelper htmlHelper, string linkText, string url, string selectedClassName)
        {
            TagBuilder liTagBuilder = new TagBuilder("li");

            Uri linkUri;
            if (!Uri.TryCreate(url, UriKind.Absolute, out linkUri))
            {
                linkUri = new Uri(htmlHelper.ViewContext.HttpContext.Request.Url, url);
            }
            if (htmlHelper.ViewContext.HttpContext.Request.Url.Equals(linkUri))
            {
                liTagBuilder.MergeAttribute("class", selectedClassName);
            }

            liTagBuilder.InnerHtml = string.Format("<a href=\"{0}\">{1}</a>", url, linkText);
            return MvcHtmlString.Create(liTagBuilder.ToString(TagRenderMode.Normal));
        }
    }
}
